<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/essay/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/essay/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/essay/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/essay/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/essay/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/essay/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/essay/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="小桃子的日常分享">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="小桃子的日常分享">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="小桃子">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/essay/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/"/>





  <title>小桃子的日常分享</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/essay/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">小桃子的日常分享</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/essay/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/essay/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/essay/2020/02/19/BFC%E8%A7%84%E5%88%99%E4%BB%A5%E5%8F%8A%E7%9B%B8%E5%BA%94%E5%BA%94%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="小桃子">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/essay/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小桃子的日常分享">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/essay/2020/02/19/BFC%E8%A7%84%E5%88%99%E4%BB%A5%E5%8F%8A%E7%9B%B8%E5%BA%94%E5%BA%94%E7%94%A8/" itemprop="url">BFC规则以及相应应用</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-02-19T15:13:17+08:00">
                2020-02-19
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="BFC简介"><a href="#BFC简介" class="headerlink" title="BFC简介"></a>BFC简介</h2><p>BFC全名为(Block Fomatting Content)，即块格式化上下文，是css渲染规则的一种。</p>
<h2 id="BFC创建方式"><a href="#BFC创建方式" class="headerlink" title="BFC创建方式"></a>BFC创建方式</h2><ul>
<li>根元素(``)</li>
<li>浮动元素（元素的 <code>float</code> 不是 <code>none</code>）</li>
<li>display值为:inline-block flow-root flex grid</li>
<li>绝对定位元素（元素的 <a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/position" target="_blank" rel="noopener"><code>position</code></a> 为 <code>absolute</code> 或 <code>fixed</code>）</li>
</ul>
<h2 id="BFC规则"><a href="#BFC规则" class="headerlink" title="BFC规则"></a>BFC规则</h2><p><strong>1.内部的Box会在垂直方向，一个接一个地放置。</strong></p>
<div style="background-color: #666666;height: 100px;width: 100px;text-align: center;line-height: 100px;color: white;font-size: 28px;">
        div1
   </div>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">background-color</span>: <span class="selector-id">#666</span>;</span><br><span class="line"><span class="selector-tag">height</span>: 100<span class="selector-tag">px</span>;</span><br><span class="line"><span class="selector-tag">width</span>: 100<span class="selector-tag">px</span>;</span><br><span class="line"><span class="selector-tag">text-align</span>: <span class="selector-tag">center</span>;</span><br><span class="line"><span class="selector-tag">line-height</span>: 100<span class="selector-tag">px</span>;</span><br><span class="line"><span class="selector-tag">color</span>: <span class="selector-tag">white</span>;</span><br><span class="line"><span class="selector-tag">font-size</span>: 28<span class="selector-tag">px</span>;</span><br></pre></td></tr></table></figure>

<div style="background-color: #999999;height: 100px;width: 100px;text-align: center;line-height: 100px;color: white;font-size: 28px;">
    div2
</div>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">background-color</span>: <span class="selector-id">#999</span>;</span><br><span class="line"><span class="selector-tag">height</span>: 100<span class="selector-tag">px</span>;</span><br><span class="line"><span class="selector-tag">width</span>: 100<span class="selector-tag">px</span>;</span><br><span class="line"><span class="selector-tag">text-align</span>: <span class="selector-tag">center</span>;</span><br><span class="line"><span class="selector-tag">line-height</span>: 100<span class="selector-tag">px</span>;</span><br><span class="line"><span class="selector-tag">color</span>: <span class="selector-tag">white</span>;</span><br><span class="line"><span class="selector-tag">font-size</span>: 28<span class="selector-tag">px</span>;</span><br></pre></td></tr></table></figure>

<p><strong>2.Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠。</strong></p>
<div style="background-color: #666;height: 100px;width: 100px;text-align: center;line-height: 100px;color: white;font-size: 28px;margin-bottom:100px;">
        div1
   </div>

<div style="background-color: #999;height: 100px;width: 100px;text-align: center;line-height: 100px;color: white;font-size: 28px;margin-top:100px;">
        div2
   </div>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div1</span><span class="selector-pseudo">:margin-bottom</span><span class="selector-pseudo">:100px</span>;</span><br></pre></td></tr></table></figure>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div2</span><span class="selector-pseudo">:margin-top</span><span class="selector-pseudo">:100px</span>;</span><br></pre></td></tr></table></figure>



<p><strong>3.每个盒子（块盒与行盒）的margin box的左边，与包含块border box的左边相接触(对于从左往右的格式化，否则相反)。即使存在浮动也是如此。</strong></p>
<div style="border-width:5px;border-color:blue;border-style:solid;width:300px">
    <div style="background-color: #666;height: 100px;width: 100px;text-align: center;line-height: 100px;color: white;font-size: 28px;float:left;margin-left:30px">
        div1
   </div>    
    <div style="background-color: #999;height: 100px;width: 100px;text-align: center;line-height: 100px;color: white;font-size: 28px;float:right;margin-right:30px">
        div2
   </div>
   </div>





<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div1</span><span class="selector-pseudo">:float</span><span class="selector-pseudo">:left</span>;</span><br><span class="line"><span class="selector-tag">margin-left</span><span class="selector-pseudo">:30px</span>;</span><br><span class="line"><span class="selector-tag">div2</span><span class="selector-pseudo">:float</span><span class="selector-pseudo">:right</span>;</span><br><span class="line"><span class="selector-tag">margin-right</span><span class="selector-pseudo">:30px</span>;</span><br></pre></td></tr></table></figure>

<p><strong>4.BFC的区域不会与float box重叠。</strong></p>
<div style="float:left;background-color: #666666;height: 100px;width: 100px;text-align: center;line-height: 100px;color: white;font-size: 28px;">
        div1
   </div>
<div style="display:flow-root;background-color: #999;height: 100px;width: 100px;text-align: center;line-height: 100px;color: white;font-size: 28px;">
        div2
</div>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div1</span><span class="selector-pseudo">:float</span><span class="selector-pseudo">:left</span>;</span><br><span class="line"><span class="selector-tag">div2</span><span class="selector-pseudo">:display</span><span class="selector-pseudo">:flow-root</span>;(触发了<span class="selector-tag">BFC</span>规则)</span><br></pre></td></tr></table></figure>

<p><strong>5.BFC就是页面上的一个隔离的独立容器，容器里面的子元素不会影响到外面的元素。反之也如此。</strong></p>
<p><strong>6.计算BFC的高度时，浮动元素也参与计算。</strong></p>
<h2 id="BFC应用"><a href="#BFC应用" class="headerlink" title="BFC应用"></a>BFC应用</h2><p>1.自适应两栏布局<br>2.可以阻止元素被浮动元素覆盖<br>3.可以包含浮动元素——清除内部浮动<br>4.分属于不同的BFC时可以阻止margin重叠</p>
<p><a href="https://blog.csdn.net/qq_38763173/article/details/104217253" target="_blank" rel="noopener">参考文档</a> <a href="https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Block_formatting_context" target="_blank" rel="noopener" style="float:right">mozilla</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  


          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">小桃子</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/essay/archives/">
              
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">小桃子</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/essay/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/essay/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/essay/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/essay/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/essay/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/essay/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/essay/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/essay/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/essay/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
